/*
 * This file (MainWindow.java) is protected by the GNU GPL licence (v2). 
 * Please read the license.txt attached for further informations. 
 * 
 */
package org.algoristes.alkwarel.gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.algoristes.alkwarel.utils.Constants;

/**
 * Main frame of the Alkwarel GUI
 * 
 * @author Xavier Gouchet
 * 
 */
public class MainWindow extends JFrame implements Constants {

	private static final long serialVersionUID = V_SERIAL_UID;

	// GUI widgets
	private JPanel mMainPanel;
	private JPanel mScriptPanel;
	private JScrollPane mImageScroll;
	private ImagePanel mImagePanel;

	/**
	 * Standard constructor, builds the widgets and menus of the main window
	 * 
	 */
	public MainWindow() {
		super();

		// Set JFrame params
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle(FULL_TITLE);
		setResizable(true);

		// Set Content
		setContentPane(getMainPanel());
		setMinimumSize(new Dimension(512, 384));
		pack();
	}

	/**
	 * Returns the main panel. If it doesn't exist, create it first.
	 * 
	 * @return the main panel
	 */
	public JPanel getMainPanel() {
		if (mMainPanel == null) {
			mMainPanel = new JPanel();
			mMainPanel.setLayout(new GridBagLayout());

			// fill with other panels
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.weightx = 1;
			gbc.weighty = 1;
			gbc.fill = GridBagConstraints.BOTH;

			gbc.gridx = 0;
			gbc.gridy = 0;
			mMainPanel.add(getImageScroll(), gbc);

			gbc.gridx = 1;
			gbc.gridy = 0;
			mMainPanel.add(getScriptPanel(), gbc);

		}
		return mMainPanel;
	}

	/**
	 * Returns the script panel. If it doesn't exist, create it first.
	 * 
	 * @return the script panel
	 */
	public JPanel getScriptPanel() {
		if (mScriptPanel == null) {
			mScriptPanel = new ScriptPanel();
		}
		return mScriptPanel;
	}

	private JScrollPane getImageScroll() {
		if (mImageScroll == null) {
			mImageScroll = new JScrollPane(getImagePanel());
		}
		return mImageScroll;
	}

	/**
	 * Returns the image panel. If it doesn't exist, create it first.
	 * 
	 * @return the image panel
	 */
	public ImagePanel getImagePanel() {
		if (mImagePanel == null)
			mImagePanel = new ImagePanel();

		return mImagePanel;
	}
}
